class Solution:
    word_dic = {"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8, "J": 9, "K": 10, "L": 11,
                "M": 12, "N": 13, "O": 14, "P": 15, "Q": 16, "R": 17, "S": 18, "T": 19, "U": 20, "V": 21, "W": 22,
                "X": 23, "Y": 24, "Z": 25}

    def leastInterval(self, tasks: list, n: int) -> int:
        work_list = [0] * 25
        for work in tasks:
            work_list[Solution.word_dic[work]] += 1
        max_work = max(work_list)
        mid_res = (n + 1) * (max_work - 1) + work_list.count(max_work)
        if n >= 25 - work_list.count(0):
            return mid_res
        else:
            return max(len(tasks), mid_res)


# class Solution:
#     """还是上面那个快"""
#     word_list = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
#                 "U", "V", "W", "X", "Y", "Z"]
#
#     def leastInterval(self, tasks: list, n: int) -> int:
#         work_list = [tasks.count(Solution.word_list[i]) for i in range(26)]
#         work_class = 25 - work_list.count(0)
#         max_work = max(work_list)
#         mid_res = (n + 1) * (max_work - 1) + work_list.count(max_work)
#         work_count = len(tasks)
#         if n >= work_class:
#             return mid_res
#         else:
#             return max(work_count, mid_res)


a = Solution()
print(a.leastInterval(tasks=["A", "A", "A", "B", "B", "B"], n=2))